/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Resources;
using System.Reflection;

namespace SilverChart.Core.Resources {
    /// <summary>
    /// Description of ChartResources.
    /// </summary>
    public class ResourcesHelper {
        private static ResourcesHelper _instance = null;

        private Dictionary<String, String> cachedResource;
        private const String NullValue = "__NULL__";

        public ResourcesHelper() {
            cachedResource = new Dictionary<String, String>();
        }

        public static ResourcesHelper Instance {
            get {
                if (_instance == null) {
                    _instance = new ResourcesHelper();
                }

                return _instance;
            }
        }

        public String this[Assembly assembly, String key] {
            get {
                String resKey = assembly.FullName + ";" + key;

                if (cachedResource.ContainsKey(resKey)) {
                    String resources = cachedResource[resKey];
                    if (resources == NullValue) {
                        return null;
                    }
                    return resources;
                }

                String res = ReadResourcesFromAssembly(assembly, key + ".xaml");
                cachedResource[resKey] = res == null ? NullValue : res; ;
                return res;
            }
        }


        public String this[String key] {
            get {
                String resources = this[Assembly.GetCallingAssembly(), key];
                if (resources == null) {
                    resources = this[this.GetType().Assembly, key];
                }
                return resources;
            }
        }

        public String ReadResourcesFromAssembly(Assembly assembly, String resFileName) {
            try {
                String assemblyName = assembly.FullName;
                assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(","));
                Uri uri = new Uri("/" + assemblyName + ";component/Resources/" + resFileName, UriKind.Relative);
                return ReadResourcesFromURL(uri);
            }
            catch {
                return null;
            }
        }

        public String ReadResourcesFromPackage(String resFileName) {
            try {
                Uri uri = new Uri(resFileName);
                return ReadResourcesFromURL(uri);
            }
            catch {
                return null;
            }
        }

        internal String ReadResourcesFromURL(Uri uri) {
            StreamResourceInfo info = Application.GetResourceStream(uri);
            if (info != null) {
                using (StreamReader sr = new StreamReader(info.Stream)) {
                    return sr.ReadToEnd();
                }
            }

            return null;
        }
    }
}
